package com.dlwx.parttimetalent.ui;


import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.GuideViewPagerAdapter;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.utils.DeviceUtils;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * 引导页面
 */
public class GuideActivity extends BaseActivity implements View.OnClickListener {
    private ViewPager vp;
    private GuideViewPagerAdapter adapter;
    private List<View> views;
    private TextView loginBtn,registerBtn;

    // 引导页图片资源
    private static final int[] pics = {R.layout.guide_view1,
            R.layout.guide_view2, R.layout.guide_view3};

    // 底部小点图片
//    private ImageView[] dots;

    // 记录当前选中位置
//    private int currentIndex;
    private RxPermissions rxpermissions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
//        StatusBarUtil.setTranslucentForImageView(GuideActivity.this, 0, null);
        views = new ArrayList<View>();
        // 初始化引导页视图列表
        for (int i = 0; i < pics.length; i++) {
            View view = LayoutInflater.from(this).inflate(pics[i], null);
            if (i == pics.length - 1) {

                loginBtn = (TextView) view.findViewById(R.id.tv_login);
                registerBtn = (TextView) view.findViewById(R.id.tv_register);

                loginBtn.setOnClickListener(this);
//                registerBtn.setOnClickListener(this);
                loginBtn.setTag("login");
//                registerBtn.setTag("register");
            }
            views.add(view);
        }
        vp = (ViewPager) findViewById(R.id.vp_guide);
        adapter = new GuideViewPagerAdapter(views);
        vp.setAdapter(adapter);
        vp.addOnPageChangeListener(new PageChangeListener());

//        initDots();
//        checkAllPermiss();
        rxpermissions=new RxPermissions(this);
        rxpermissions.request(Manifest.permission.READ_PHONE_STATE,Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if(!aBoolean){
                            android.os.Process.killProcess(android.os.Process.myPid());
                            System.exit(0);
                        }else{
                            String id= DeviceUtils.getDeviceId(GuideActivity.this);
                            SPUtils.put(GuideActivity.this,Constant.DEVICE_ID,id);
                            OkgoUtil.getApiToken(GuideActivity.this,id);
                        }
                    }
                });
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 如果切换到后台，就设置下次不进入功能引导页
        SPUtils.put(GuideActivity.this, Constant.First_START, false);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initDots() {
//        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
//        dots = new ImageView[pics.length];
//        // 循环取得小点图片
//        for (int i = 0; i < pics.length; i++) {
//            // 得到一个LinearLayout下面的每一个子元素
//            dots[i] = (ImageView) ll.getChildAt(i);
//            dots[i].setEnabled(false);// 都设为灰色
//            dots[i].setOnClickListener(this);
//            dots[i].setTag(i);// 设置位置tag，方便取出与当前位置对应
//        }
//        currentIndex = 0;
//        dots[currentIndex].setEnabled(true); // 设置为白色，即选中状态

    }

    /**
     * 设置当前view
     *
     * @param position
     */
    private void setCurView(int position) {
        if (position < 0 || position >= pics.length) {
            return;
        }
        vp.setCurrentItem(position);
    }

    /**
     * 设置当前指示点
     *
     * @param position
     */
    private void setCurDot(int position) {
//        if (position < 0 || position > pics.length || currentIndex == position) {
//            return;
//        }
////        dots[position].setEnabled(true);
////        dots[currentIndex].setEnabled(false);
//        currentIndex = position;
    }

    @Override
    public void onClick(View v) {
        if (v.getTag().equals("login")) {
            enterMainActivity();
            return;
        }else if(v.getTag().equals("register")){
            enterMainActivity();
            return;
        }


//        int position = (Integer) v.getTag();
//        setCurView(position);
//        setCurDot(position);
    }


    private void enterMainActivity() {
        SPUtils.put(GuideActivity.this, Constant.First_START, false);
        Intent intent = new Intent(GuideActivity.this, SplashActivity.class);
        startActivity(intent);
        finish();
    }

    private class PageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int position) {
        }

        @Override
        public void onPageScrolled(int position, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int position) {
            // 设置底部小点选中状态
//            setCurDot(position);
        }
    }
    //-----------------------------------------------------------------------------
}
